/*
' Copyright (c) 2012  DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using System.Data;
using DotNetNuke.Modules.DNNPagination.Components;


namespace DotNetNuke.Modules.DNNPagination
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Settings class manages Module Settings
    /// 
    /// Typically your settings control would be used to manage settings for your module.
    /// There are two types of settings, ModuleSettings, and TabModuleSettings.
    /// 
    /// ModuleSettings apply to all "copies" of a module on a site, no matter which page the module is on. 
    /// 
    /// TabModuleSettings apply only to the current module on the current page, if you copy that module to
    /// another page the settings are not transferred.
    /// 
    /// If you happen to save both TabModuleSettings and ModuleSettings, TabModuleSettings overrides ModuleSettings.
    /// 
    /// Below we have some examples of how to access these settings but you will need to uncomment to use.
    /// 
    /// Because the control inherits from DNNPaginationSettingsBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class Settings : DNNPaginationSettingsBase
    {

        #region Base Method Implementations

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// LoadSettings loads the settings from the Database and displays them
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void LoadSettings()
        {
            try
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Load(cObjectController.getObject(ModuleId));

                    DataRow settings = dt.Rows[0];
                    if (dt.Rows.Count > 0)
                    {
                        if (settings["srcTabId"].ToString() == "-1")
                            DropDownList1.SelectedIndex = 0;
                        else
                            DropDownList1.SelectedIndex = 1;

                        TextBox1.Text = settings["TitleStyle"].ToString();
                        TextBox2.Text = settings["ItemStyle"].ToString();
                        TextBox3.Text = settings["SeparatorStyle"].ToString();
                        TextBox4.Text = settings["ItemsPerPage"].ToString();
                    }
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// UpdateSettings saves the modified settings to the Database
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void UpdateSettings()
        {
            try
            {
                cObject o = new cObject();
                if (DropDownList1.SelectedIndex == 0)
                    o.srcTabId = -1;
                else
                    o.srcTabId = 0;
                o.moduleId = ModuleId;
                o.srcPane = null;
                o.TitleStyle = TextBox1.Text;
                o.ItemStyle = TextBox2.Text;
                o.SeparatorStyle = TextBox3.Text;
                o.ItemsPerPage = Convert.ToInt32(TextBox4.Text);

                cObjectController.updateObject(o);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

    }

}

